/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.tabbedproperties.section;

import org.eclipse.jface.resource.StringConverter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.FontDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.views.properties.tabbed.ITabbedPropertyConstants;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;

import com.cea.papyrus.core.editpart.GraphEdgeEditPart;
import com.cea.papyrus.core.editpart.GraphNodeEditPart;
import com.cea.papyrus.core.editpart.GraphNodeWithSemanticEditPart;
import com.cea.papyrus.core.tabbedproperties.PropertyViewSection;
import com.cea.papyrus.diagraminterchange2.di2.GraphElement;

/**
 * 
 */
public class FontSection extends PropertyViewSection {

    /**
     * 
     */
    protected Text fontText;

    /**
     * 
     */
    protected Button fontButton;

    /**
     * 
     */
    protected SelectionListener fontButtonListener;

    /**
     * @param tabbedPropertySheetPage
     * @param parent
     * @see org.eclipse.ui.views.properties.tabbed.ITabbedPropertySection#createControls(org.eclipse.swt.widgets.Composite,
     *      org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage)
     */
    @Override
    public void createControls(Composite parent, TabbedPropertySheetPage tabbedPropertySheetPage) {
        super.createControls(parent, tabbedPropertySheetPage);
        Composite composite = getWidgetFactory().createFlatFormComposite(parent);
        FormData data;

        Shell shell = new Shell();
        GC gc = new GC(shell);
        gc.setFont(shell.getFont());
        Point point = gc.textExtent("");//$NON-NLS-1$
        int buttonHeight = point.y + 5;
        gc.dispose();
        shell.dispose();

        CLabel fontLabel = getWidgetFactory().createCLabel(composite, "Font:"); //$NON-NLS-1$
        fontText = getWidgetFactory().createText(composite, ""); //$NON-NLS-1$
        fontText.setEditable(false);
        fontButton = getWidgetFactory().createButton(composite, "Change...", SWT.PUSH); //$NON-NLS-1$
        fontButton.addSelectionListener(fontButtonListener = new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent event) {
                FontDialog ftDialog =
                        new FontDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow()
                                .getShell());

                FontData oldFontdata = null;
                if (graphicalElement instanceof GraphNodeEditPart) {
                    oldFontdata =
                            new FontData(((GraphNodeWithSemanticEditPart) graphicalElement)
                                    .getGraphNode().getFontFamily(),
                                    ((GraphNodeWithSemanticEditPart) graphicalElement)
                                            .getGraphNode().getFontSize(), SWT.NORMAL);
//                } else if (graphicalElement instanceof SeDiSubpartEditPart
//                        || graphicalElement instanceof AbstractLinkEditPart) {
//                    if (graphicalElement.getModel() instanceof GraphElement) {
//                        GraphElement graphElement = (GraphElement) graphicalElement.getModel();
//                        oldFontdata =
//                                new FontData(graphElement.getFontFamily(), graphElement
//                                        .getFontSize(), SWT.NORMAL);
//                    }
                } else if (graphicalElement instanceof GraphEdgeEditPart) {
                    oldFontdata =
                            new FontData(((GraphEdgeEditPart) graphicalElement).getGraphEdge()
                                    .getFontFamily(), ((GraphEdgeEditPart) graphicalElement)
                                    .getGraphEdge().getFontSize(), SWT.NORMAL);

                }

                ftDialog.setFontData(oldFontdata);
                FontData fData = ftDialog.open();
                if (fData != null) {

                    if (graphicalElement instanceof GraphNodeEditPart) {
                        ((GraphNodeWithSemanticEditPart) graphicalElement).getGraphNode()
                                .setFontFamily(fData.getName());
                        ((GraphNodeWithSemanticEditPart) graphicalElement).getGraphNode()
                                .setFontSize(fData.getHeight());
//                    } else if (graphicalElement instanceof SeDiSubpartEditPart
//                            || graphicalElement instanceof AbstractLinkEditPart) {
//                        if (graphicalElement.getModel() instanceof GraphElement) {
//                            GraphElement graphElement = (GraphElement) graphicalElement.getModel();
//                            graphElement.setFontFamily(fData.getName());
//                            graphElement.setFontSize(fData.getHeight());
//                        }
                    } else if (graphicalElement instanceof GraphEdgeEditPart) {
                        ((GraphEdgeEditPart) graphicalElement).getGraphEdge().setFontFamily(
                                fData.getName());
                        ((GraphEdgeEditPart) graphicalElement).getGraphEdge().setFontSize(
                                fData.getHeight());

                    }

                    refresh();
                }
            }
        });

        data = new FormData();
        data.left = new FormAttachment(0, 0);
        data.right = new FormAttachment(fontText, -ITabbedPropertyConstants.HSPACE);
        data.top = new FormAttachment(0, 0);
        fontLabel.setLayoutData(data);

        data = new FormData();
        data.left = new FormAttachment(0, STANDARD_LABEL_WIDTH);
        data.right = new FormAttachment(fontButton, -ITabbedPropertyConstants.HSPACE);
        data.top = new FormAttachment(0, 0);
        fontText.setLayoutData(data);

        data = new FormData();
        data.right = new FormAttachment(100, 0);
        data.top = new FormAttachment(0, 0);
        data.height = buttonHeight;
        fontButton.setLayoutData(data);
    }

    /*
     * @see org.eclipse.ui.views.properties.tabbed.view.ITabbedPropertySection#refresh()
     */
    /**
     * 
     */
    @Override
    public void refresh() {
        if (!fontText.isDisposed() && graphicalElement != null) {
            FontData fontdata = null;
            if (graphicalElement instanceof GraphNodeEditPart) {
                fontdata = new FontData(
                		((GraphNodeWithSemanticEditPart) graphicalElement).getGraphNode().getFontFamily(),
                		((GraphNodeWithSemanticEditPart) graphicalElement).getGraphNode().getFontSize(), SWT.NORMAL);
                // } else if (graphicalElement instanceof SeDiSubpartEditPart
                // || graphicalElement instanceof AbstractLinkEditPart) {
                // if (graphicalElement.getModel() instanceof GraphElement) {
                // GraphElement graphElement = (GraphElement) graphicalElement.getModel();
                // fontdata =
                // new FontData(graphElement.getFontFamily(), graphElement.getFontSize(),
                // SWT.NORMAL);
                // }
            } else if (graphicalElement.getModel() instanceof GraphElement) {
                GraphElement graphElement = (GraphElement) graphicalElement.getModel();
                fontdata =
                        new FontData(graphElement.getFontFamily(), graphElement.getFontSize(),
                                SWT.NORMAL);
            } else if (graphicalElement instanceof GraphEdgeEditPart) {
                fontdata =
                        new FontData(((GraphEdgeEditPart) graphicalElement).getGraphEdge()
                                .getFontFamily(), ((GraphEdgeEditPart) graphicalElement)
                                .getGraphEdge().getFontSize(), SWT.NORMAL);

            }
            fontText.setText(StringConverter.asString(fontdata));
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.cea.papyrus.core.ui.properties.tabbed.PropertyViewSection#dispose()
     */
    /**
     * 
     */
    public void dispose() {
        super.dispose();
        if (fontButton != null && !fontButton.isDisposed())
            fontButton.removeSelectionListener(fontButtonListener);
    }

}
